//
//  MyLetterViewController.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/2/28.
//  Copyright © 2017年 金橙云. All rights reserved.
//
#define O_WIDTH [UIScreen mainScreen].bounds.size.width
#import "MyLetterViewController.h"

#import "MessageCell.h"
#import "TodoHttpManager.h"
#import "MessageXVC.h"
@interface MyLetterViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    UIView *_aView;
    NSMutableArray *_messageArr;
}
@property(nonatomic,strong)UITableView *tabView;
@end


@implementation MyLetterViewController

-(void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDGetMineMessage:) name:TDGetMineMessage object:nil];
    
    [_manager userGetMineMessage];
}

-(void)didTDGetMineMessage:(NSNotification *)notifi
{
    if (notifi.object != 0)
    {
        int status = [[notifi.object objectForKey:@"status"] intValue];
        if (status == 1)
        {
            _messageArr = [notifi.object objectForKey:@"data"];
            [_tabView reloadData];
        }
    }
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDGetMineMessage object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的站内信";
    self.view.backgroundColor = BgColor;
    _manager = [TodoHttpManager sharedManager];
    _messageArr = [NSMutableArray arrayWithCapacity:0];
    _aView = [[UIView alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight, B_WIDTH, 30)];
    _aView.backgroundColor = [UIColor colorWithHexString:@"#eb6100"];
    [self.view addSubview:_aView];
  
    NSInteger a = 70;
    NSInteger b = (O_WIDTH-15*2-a*4)/3;
    
    UILabel *oneLab = [[UILabel alloc] init];
    oneLab.text = @"发送时间";
    oneLab.textAlignment = NSTextAlignmentCenter;
    oneLab.font = [UIFont systemFontOfSize:12];
    oneLab.textColor =[UIColor whiteColor];
    [_aView addSubview:oneLab];
    [oneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(a, 20));
        make.left.equalTo(_aView.mas_left).offset(15);
        make.top.equalTo(_aView.mas_top).offset(5);
    }];
    
    UILabel *tLab = [[UILabel alloc] init];
    tLab.text = @"发件主题";
    tLab.textAlignment = NSTextAlignmentCenter;
    tLab.font = [UIFont systemFontOfSize:12];
    tLab.textColor =[UIColor whiteColor];
    [_aView addSubview:tLab];
    [tLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(a, 20));
        make.left.equalTo(oneLab.mas_right).offset(b);
        make.top.equalTo(_aView.mas_top).offset(5);
    }];

    
    UILabel *sLab = [[UILabel alloc] init];
    sLab.text = @"发件人";
    sLab.textAlignment = NSTextAlignmentCenter;
    sLab.font = [UIFont systemFontOfSize:12];
    sLab.textColor =[UIColor whiteColor];
    [_aView addSubview:sLab];
    [sLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(a, 20));
        make.left.equalTo(tLab.mas_right).offset(b);
        make.top.equalTo(_aView.mas_top).offset(5);
    }];

    UILabel *fLab = [[UILabel alloc] init];
    fLab.text = @"操作";
    fLab.textAlignment = NSTextAlignmentCenter;
    fLab.font = [UIFont systemFontOfSize:12];
    fLab.textColor =[UIColor whiteColor];
    [_aView addSubview:fLab];
    [fLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(a, 20));
        make.right.equalTo(_aView.mas_right).offset(-15);
        make.top.equalTo(_aView.mas_top).offset(5);
    }];

    
    

    
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight+30, B_WIDTH, B_HEIGHT-(LL_StatusBarAndNavigationBarHeight+30)) style:UITableViewStylePlain];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.rowHeight = 35;
    _tabView.backgroundColor = BgColor;
    _tabView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:_tabView];
    
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _messageArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellA = @"Cell";
    MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:cellA];
    if (cell == nil)
    {
        cell = [[MessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellA];
    }
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    if ([cell isKindOfClass:[MessageCell class]])
    {
        if (_messageArr.count !=0)
        {
            NSDictionary *dict = [_messageArr objectAtIndex:indexPath.row];
            [(MessageCell *)cell setUpMessageCellWithTime:dict[@"send_time"] withZhuTi:dict[@"zhuti"] withPeople:dict[@"name"] withXiang:dict[@"button"]];
        }
        
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageXVC *messageVC = [[MessageXVC alloc] init];
    messageVC.dataStr = [[_messageArr objectAtIndex:indexPath.row] objectForKey:@"send_time"];
    messageVC.zhuStr = [[_messageArr objectAtIndex:indexPath.row] objectForKey:@"zhuti"];
    messageVC.contentStr = [[_messageArr objectAtIndex:indexPath.row] objectForKey:@"content"];
    [self.navigationController pushViewController:messageVC animated:NO];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
